package com.wofish.goods.service.impl;

import com.wofish.common.core.text.Convert;
import com.wofish.goods.domain.GoodsCat;
import com.wofish.goods.domain.GoodsType;
import com.wofish.goods.dto.GoodsCatZtree;
import com.wofish.goods.mapper.GoodsCatMapper;
import com.wofish.goods.mapper.GoodsTypeMapper;
import com.wofish.goods.service.IGoodsCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类 服务层实现
 *
 * @author wofish
 * @date 2019-04-09
 */
@Service
public class GoodsCatServiceImpl implements IGoodsCatService {
    @Autowired
    private GoodsCatMapper goodsCatMapper;

    @Autowired
    private GoodsTypeMapper goodsTypeMapper;

    /**
     * 查询商品分类信息
     *
     * @param catId 商品分类ID
     * @return 商品分类信息
     */
    @Override
    public GoodsCat selectGoodsCatById(Long catId) {
        return goodsCatMapper.selectGoodsCatById(catId);
    }

    /**
     * 查询商品分类列表
     *
     * @param goodsCat 商品分类信息
     * @return 商品分类集合
     */
    @Override
    public List<GoodsCat> selectGoodsCatList(GoodsCat goodsCat) {
        return goodsCatMapper.selectGoodsCatList(goodsCat);
    }

    /**
     * 新增商品分类
     *
     * @param goodsCat 商品分类信息
     * @return 结果
     */
    @Override
    public int insertGoodsCat(GoodsCat goodsCat) {
        GoodsCat info = goodsCatMapper.selectGoodsCatById(goodsCat.getParentId());
        goodsCat.setAncestors(info.getAncestors() + "," + goodsCat.getParentId());
        return goodsCatMapper.insertGoodsCat(goodsCat);
    }

    /**
     * 修改商品分类
     *
     * @param goodsCat 商品分类信息
     * @return 结果
     */
    @Override
    public int updateGoodsCat(GoodsCat goodsCat) {
        if (goodsCat.getTypeId() != null) {
            GoodsType goodsType = goodsTypeMapper.selectGoodsTypeById(goodsCat.getTypeId());
            goodsCat.setTypeName(goodsType.getName());
        }
        return goodsCatMapper.updateGoodsCat(goodsCat);
    }

    /**
     * 删除商品分类对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGoodsCatByIds(String ids) {
        return goodsCatMapper.deleteGoodsCatByIds(Convert.toStrArray(ids));
    }

    //	@DataScope(tableAlias = "c")
    @Override
    public List<GoodsCatZtree> selectCatTree(GoodsCat goodsCat) {
        List<GoodsCat> goodsCatList = goodsCatMapper.selectCatList(goodsCat);
        List<GoodsCatZtree> ztrees = initZtree(goodsCatList);
        return ztrees;
    }

    /**
     * 对象转部门树
     *
     * @param goodsCatList
     * @return 树结构列表
     */
    public List<GoodsCatZtree> initZtree(List<GoodsCat> goodsCatList) {

        List<GoodsCatZtree> ztrees = new ArrayList<>();
        for (GoodsCat goodsCat : goodsCatList) {
            GoodsCatZtree ztree = new GoodsCatZtree();
            ztree.setId(goodsCat.getCatId());
            ztree.setpId(goodsCat.getParentId());
            ztree.setName(goodsCat.getCatName());
            ztree.setTitle(goodsCat.getCatName());
            ztree.setTypeId(goodsCat.getTypeId());
            ztree.setTypeName(goodsCat.getTypeName());
            ztrees.add(ztree);
        }
        return ztrees;
    }


}
